﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Entity;
namespace Web.admin.news
{
    public partial class edit :System.Web.UI.Page
    {
        UI.ManagePage managePage = new UI.ManagePage();
        BLL.NewsContent newscontent = new BLL.NewsContent();
        BLL.NewsClass newsClass = new BLL.NewsClass();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                //对下拉列表的数据绑定
                DDLBind("", DDLClass);
                if (Request.QueryString["id"] != null && Request.QueryString["id"] != " ")
                {
                    List_Bind(Request.QueryString["id"]);
                }
            }
        }
        /// <summary>
        /// 保存按钮点击
        /// </summary>
        /// <param name="sender">对象</param>
        /// <param name="e">事件</param>
        protected void btn_Save_Click(object sender, EventArgs e) {
            BoNewsContent newsModel = new BoNewsContent();
            newsModel.NewsTitle = TitleTxt.Value;
            newsModel.NewsClassId = DDLClass.SelectedValue;
            newsModel.NewsAuthor = FromTxt.Value;
            newsModel.NewsImgUrl = txtImgUrl.Value;
            newsModel.NewsIsShow = bool.Parse(AttributeDDL.SelectedValue);
            newsModel.NewsArticle = KindEditor.Text;
            if (AttributeDDL.SelectedValue == "True")
                newsModel.NewsIsShow = true;
            else
                newsModel.NewsIsShow = false;
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                newsModel.NewsId = Request.QueryString["id"];
                newscontent.UpdateRow(newsModel);
                Response.Write("<script>alert('修改成功');window.location.href='list.aspx';</script>");
            }
            else
            {
                newscontent.Insert(newsModel);
                Response.Write("<script>alert('添加成功');window.location.href='list.aspx';</script>");
            }
        }
        /// <summary>
        /// 对列表的填充
        /// </summary>
        /// <param name="id">数据的id</param>
        protected void List_Bind(string id) {
           BoNewsContent newsModel = newscontent.GetNewsSingle(id);
           if (newsModel != null)
           {
               DDLClass.SelectedValue = newsModel.NewsClassId;
               TitleTxt.Value = newsModel.NewsTitle;
               FromTxt.Value = newsModel.NewsAuthor;
               txtImgUrl.Value = newsModel.NewsImgUrl;
               KindEditor.Text =newsModel.NewsArticle;
               AttributeDDL.SelectedValue =newsModel.NewsIsShow.ToString();
           }
        }

        /// <summary>
        /// 对DropDownList的绑定
        /// </summary>
        /// <param name="firstStr">第一行显示的数据</param>
        /// <param name="ddl">DropDownList控件</param>
        protected void DDLBind(string firstStr, DropDownList ddl)
        {
            if (!string.IsNullOrEmpty(firstStr))
            {
                ddl.Items.Add(new ListItem(firstStr, ""));
            }
            DataTable table = newsClass.GetNewsClass();
            foreach (DataRow dr in table.Rows)
            {
                ddl.Items.Add(new ListItem(dr["NCTitle"].ToString(), dr["NCId"].ToString()));
            }
        }
    }
}